#include "bsp.h"
#include "os_obj.h"
#include "ff.h"
#include "sys_init.h"
#include "common.h"
#include "str_ext.h"
#include "systask.h"

#define WR_PDF 		0

FIL fil;
FRESULT fst;
DIR dir;
FILINFO fil_info;

void pdf_author_demo(int (*disk_out)(uint8_t *buf, int len));
int sd_spdf_disk_out(uint8_t *buf, int len);

uu8 mf_scan_files(char * path);

int32_t SDH_Open_Disk(SDH_T *sdh, uint32_t u32CardDetSrc);

TCHAR       sd_path[] = { '1', ':', 0 };    /* SD drive started from 0 */

const char sd_txt[40] OBJ_ALIGN(8)= "m484 sdcard file test !\n";

UINT bk_len;
uint8_t sd_test_ok;

/*****************************************************************************
* @brief   sd_test.
* @param   argument
* @return  none
* @ Pass/ Fail criteria: none
*****************************************************************************/
void sd_test(void)
{
	if(bsp.module.sd1_ready)
	{
		char f_op = 0;
		int wrcnt = 1000;
		
		if(!sd_test_ok)
		{
			tx_mutex_get(os_obj.mid_file, TX_WAIT_FOREVER);
			
			SDH_Open_Disk(SDH1, CardDetect_From_GPIO);
			
			while(!sd_test_ok && SDH_CardDetection(SDH1))
			{
				// path
				f_chdrive(sd_path);          
				
				mf_scan_files("/");
				
				f_chdrive(sd_path); 
				
				#if WR_PDF > 0
				
				fst = f_open(&fil, "m484_sd.pdf", FA_OPEN_ALWAYS | FA_WRITE);
				
				if(fst != FR_OK) break;
				
				pdf_author_demo(sd_spdf_disk_out);
				
				f_op = 1;
				
				if(fst == FR_OK) file_dbg_puts("sd file write sucess !\n");
				else file_dbg_puts("sd file write fail !\n");
				
				#endif
				
				sd_test_ok = 1;
			}
			
			if(f_op) f_close(&fil);
			
			SDH_Close_Disk(SDH1);

			tx_mutex_put(os_obj.mid_file);			
		}
	}
	else
	{
		sd_test_ok = 0;
	}
}

/*****************************************************************************
* @brief   sd_spdf_disk_out
* @param   argument
* @return  none
* @ Pass/ Fail criteria: none
*****************************************************************************/
int sd_spdf_disk_out(uint8_t *buf, int len)
{
	UINT FsResdLen;
	
	fst = f_write(&fil, buf, len, &FsResdLen);
	
	return len;
}


/*****************************************************************************
* @brief   mf_scan_files
* @param   path
* @return  none
* @ Pass/ Fail criteria: none
*****************************************************************************/
uu8 mf_scan_files(char * path)
{
    FRESULT res;
    char *fn;   /* This function is assuming non-Unicode cfg. */
    char *p; 
	int16_t site, pdf_cnt = 0;
	
#if _USE_LFN
    fileinfo.lfsize = _MAX_LFN * 2 + 1;
    fileinfo.lfname = malloc(fileinfo.lfsize);
#endif
 
    res = f_opendir(&dir,(const char*) path);
	
    if (res == FR_OK)
    {
        while(1)
        {
            res = f_readdir(&dir, &fil_info); 
			
            if (res != FR_OK || fil_info.fname[0] == 0) break;
 
#if _USE_LFN
			fn = *fileinfo.lfname ? fileinfo.lfname : fileinfo.fname;
#else
			fn = fil_info.fname;                  
#endif                                               /* It is a file. */
			if(fil_info.fattrib & AM_DIR)
			{
				dbg_print("fonund dir %s \n", fn); 
			}
			else
			{
				dbg_print("fonund file %s \n", fn);
				site = str_nsearch_ch(fn,'.');
				
				if(site > 0)
				{
					if(0 == comp_string((void*)&fn[site],(void*)".PDF\0", 5))
					{
						pdf_cnt++;
					}
				}
			}			
        }
		
		dbg_print("fonund pdf file amount : %d \n", pdf_cnt);
    }
	
    return res;
}